package com.example.eduservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.eduservice.client.VodClient;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.mapper.EduVideoMapper;
import com.example.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-26
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

        @Autowired
        private VodClient vodClient;


    @Override
    public void removeVideoByid(String courseId) {
        //TODO   删除小节删除对应的视频

        //根据课程id查询所有的视频的id
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.select("video_source_id");
        List<EduVideo> videoList=baseMapper.selectList(wrapperVideo);

        List<String> Videoid=new ArrayList<>();

        //lsit<eduvideo> 转成list《string》
        for(int i=0;i<videoList.size();i++){
            EduVideo eduVideo = videoList.get(i);

            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)) {
                //放到list集合
                Videoid.add(videoSourceId);
            }
        }
        if(Videoid.size()>0) {
            //调用方法删除多个视频
            vodClient.deleteBatchVideo(Videoid);
        }


        //构造条件
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);

        baseMapper.delete(wrapper);
    }
}
